package com.lagou.edu.tpmonitor.filter;

import com.lagou.edu.tpmonitor.manager.PTMonitorManager;
import com.lagou.edu.tpmonitor.model.TimeMonitor;
import org.apache.dubbo.rpc.*;

/**
 * @author liangzj
 * @date 2021/5/19 21:58
 */
public class TPMonitorFilter implements Filter {

    private PTMonitorManager ptMonitorManager = new PTMonitorManager();

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String methodName = invocation.getMethodName();

        TimeMonitor timeMonitor = new TimeMonitor();
        timeMonitor.start();

        Result result = invoker.invoke(invocation);

        timeMonitor.stop();

        ptMonitorManager.recordMonitor(methodName, timeMonitor);
        return result;
    }

}
